package org.linlinjava.litemall.wx.web;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.core.validator.Sort;
import org.linlinjava.litemall.db.domain.PiccPatient;
import org.linlinjava.litemall.db.service.PiccPatientService;
import org.linlinjava.litemall.wx.service.WxPiccPatientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("wx/patients")
@Validated
public class WxPiccPatientController {
    private final Log logger = LogFactory.getLog(WxPiccPatientController.class);
    @Autowired
    PiccPatientService piccPatientService;

    @Autowired
    WxPiccPatientService wxPiccPatientService;

    /**
     * list路由，列出一个护士名下，所有的病人列表
     * @param nurseId
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @return
     */
    @GetMapping("list")
    public Object getPatients(@RequestParam(defaultValue = "0") Integer nurseId,
                              @RequestParam(defaultValue = "1") Integer page,
                              @RequestParam(defaultValue = "10") Integer limit,
                              @Sort @RequestParam(defaultValue = "id") String sort,
                              @RequestParam(defaultValue = "desc") String order){
        logger.debug("enter list");
        List<PiccPatient> mList = piccPatientService.getPatientsByNurseId(nurseId, page, limit, sort, order);
        logger.debug(mList);
        return ResponseUtil.ok(mList);
    }

    /**
     * 返回一个病人的所有字段，根据id查询
     * @param id
     * @return
     */
    @GetMapping("detail")
    public Object getDetail(@RequestParam Integer id){
        PiccPatient p = piccPatientService.getDetailById(id);
        return ResponseUtil.ok(p);
    }

    /**
     * 添加病人
     * @param patient
     * @return
     */
    @PostMapping("create")
    public Object add(@RequestBody PiccPatient patient){
        logger.debug("enter patient create: "+patient);
        PiccPatient p = wxPiccPatientService.add(patient);
        return (p != null) ? ResponseUtil.ok(p) : ResponseUtil.fail(402, "patient not exist");
    }

    /**
     *  验证病人
     * @param id
     * @return
     */
    @PostMapping("verify")
    public Object verify(@RequestBody Integer id){
        PiccPatient patient = piccPatientService.getDetailById(id);
        logger.debug("enter patient verify: "+patient);
        patient.setStatus(true);
        PiccPatient p = piccPatientService.update(patient);
        return ResponseUtil.ok(p);
    }
}
